#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'sszheng'

from apiUrls import API_ALL
import requests
import json
# 脚本类，组合工具参数进行请求

def req():
    apikeys = API_ALL.keys()
    print(apikeys)
    for key in apikeys:
        apiname = key
        url = API_ALL[key]['url']
        type = API_ALL[key]['type']
        params = API_ALL[key]['params']
        headers = API_ALL[key]['headers']
        times = API_ALL[key]['times']
        
        if type == 'post':
            totalXyjs = 0;
            for x in range(times):
                print("=======post "+apiname+"=======")
                response = requests.post(url, data = json.dumps(params), headers = headers)
                fhcode = response.status_code
                xysj = round(response.elapsed.total_seconds(), 2) 
                totalXyjs += xysj
                print("响应code: " + str(fhcode) + "  响应时间: " + str(xysj) )
            avgXysj =  round(totalXyjs / times, 2)
            print("平均响应时间: " + str(avgXysj))
        if type == 'get':
            totalXyjs = 0;
            for x in range(times):
                print("=======get "+apiname+"=======")
                response = requests.get(url=url, data = params)
                fhcode = str(response.status_code)
                xysj = round(response.elapsed.total_seconds(), 2) 
                totalXyjs += xysj
                print("响应code: " + str(fhcode) + "  响应时间: " + str(xysj) )
            avgXysj =  round(totalXyjs / times, 2)
            print("平均响应时间: " + str(avgXysj))